package com.blrs.service.project.afterSales.impl;

import java.util.*;

import com.blrs.common.utils.ReflectionUtils;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.common.utils.StringUtils;
import com.blrs.service.project.afterSales.IProjectSalesService;
import com.blrs.system.mapper.SysUploadFileMapper;
import com.blrs.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.blrs.mapper.ProjectSalesMapper;
import com.blrs.domain.ProjectSales;

/**
 * 项目售后Service业务层处理
 * 
 * @author yang
 * @date 2023-12-07
 */
@Service
public class ProjectSalesServiceImpl implements IProjectSalesService
{
    @Autowired
    private ProjectSalesMapper projectSalesMapper;
    @Autowired
    private SysUploadFileMapper sysUploadFileMapper;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询项目售后
     * 
     * @param afterSalesId 项目售后主键
     * @return 项目售后
     */
    @Override
    public ProjectSales selectProjectSalesByAfterSalesId(String afterSalesId)
    {
        ProjectSales projectSales = projectSalesMapper.selectProjectSalesByAfterSalesId(afterSalesId);
        projectSales.setUrl(sysUploadFileMapper.selectUrlByCode("sales",projectSales.getUploadFile()));
        if(StringUtils.isNotEmpty(projectSales.getDeliverables())){
            String[] split = projectSales.getDeliverables().split(",");
            projectSales.setCheckList(new ArrayList<>(Arrays.asList(split)));
            return projectSales;
        }else {
            projectSales.setCheckList(new ArrayList<>());
        }
        return projectSales;
    }

    /**
     * 查询项目售后列表
     * 
     * @param projectSales 项目售后
     * @return 项目售后
     */
    @Override
    public List<ProjectSales> selectProjectSalesList(ProjectSales projectSales)
    {
        projectSales.setBelongTo(SecurityUtils.getOrgId());
        List<ProjectSales> projectSales1 = projectSalesMapper.selectProjectSalesList(projectSales);
        for (ProjectSales sales : projectSales1) {
            sales.setUrl(sysUploadFileMapper.selectUrlByCode("sales",sales.getUploadFile()));
        }
        return projectSales1;
    }

    /**
     * 新增项目售后
     * 
     * @param projectSales 项目售后
     * @return 结果
     */
    @Override
    public int insertProjectSales(ProjectSales projectSales) throws NoSuchFieldException, IllegalAccessException {
        projectSales.setAfterSalesId(UUID.randomUUID().toString().replaceAll("-",""));
        projectSales.setCreateId(String.valueOf(SecurityUtils.getUserId()));
        projectSales.setBelongTo(SecurityUtils.getOrgId());
       projectSales.setOperateId(SecurityUtils.getUserId());
       projectSales.setCreateId(String.valueOf(SecurityUtils.getUserId()));
       projectSales.setOperateTime(new Date());
       projectSales.setOrganizationId(SecurityUtils.getOrgId());
       projectSales.setOrganizationName(SecurityUtils.getOrgName());
       projectSales.setDepartmentId(SecurityUtils.getDeptId());

        // 将StringBuilder转换为String
       projectSales.setDeliverables(convertToStr(projectSales.getCheckList()));
        return  projectSalesMapper.insertProjectSales(projectSales);
    }

    //交付物数组转字符串
    private String convertToStr(ArrayList<String> checkList) {
       if (StringUtils.isNotEmpty(checkList)){
           StringBuilder stringBuilder = new StringBuilder();

           for (String element : checkList) {
               stringBuilder.append(element).append(",");
           }
           // 删除最后的逗号和空格
           if (stringBuilder.length() > 0) {
               stringBuilder.setLength(stringBuilder.length() - 1);
           }
           return stringBuilder.toString();
       }else {
           return null;
       }
    }

    /**
     * 修改项目售后
     * 
     * @param projectSales 项目售后
     * @return 结果
     */
    @Override
    public int updateProjectSales(ProjectSales projectSales)
    {
        // 将StringBuilder转换为String
        projectSales.setDeliverables(convertToStr(projectSales.getCheckList()));
        return projectSalesMapper.updateProjectSales(projectSales);
    }

    /**
     * 批量删除项目售后
     * 
     * @param afterSalesIds 需要删除的项目售后主键
     * @return 结果
     */
    @Override
    public int deleteProjectSalesByAfterSalesIds(String[] afterSalesIds)
    {
        return projectSalesMapper.deleteProjectSalesByAfterSalesIds(afterSalesIds);
    }

    /**
     * 删除项目售后信息
     * 
     * @param afterSalesId 项目售后主键
     * @return 结果
     */
    @Override
    public int deleteProjectSalesByAfterSalesId(String afterSalesId)
    {
        return projectSalesMapper.deleteProjectSalesByAfterSalesId(afterSalesId);
    }

    @Override
    public void updateClassIds(String[] enterIds, String classId) {

        projectSalesMapper.updateClassByIds(enterIds,classId);
    }
}
